package tx.bj12320.task;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;

import tx.bj12320.model.SmsInfo;
import tx.bj12320.model.TszbProcessInfo;
import tx.bj12320.model.UserInfo;
import tx.bj12320.model.enums.SysUserTypeEnum;
import tx.bj12320.service.OrgInfoManager;
import tx.bj12320.service.SmsInfoManager;
import tx.bj12320.service.TszbProcessInfoManager;
import tx.bj12320.service.UserInfoManager;
import tx.bj12320.util.WebUtils;
import tx.bj12320.vo.query.TszbProcessInfoQuery;
import tx.bj12320.vo.query.UserInfoQuery;

@Service
@Transactional
public class QueryUnDealTszbProcessInfoListTask {
	protected Log log = LogFactory.getLog(getClass());
	@Autowired
	private SmsInfoManager smsInfoManager;
	public void setSmsInfoManager(SmsInfoManager manager) {
		this.smsInfoManager = manager;
	}
	@Autowired
	private UserInfoManager userInfoManager;
	public void setUserInfoManager(UserInfoManager manager) {
		this.userInfoManager = manager;
	}
	@Autowired
	private OrgInfoManager orgInfoManager;
	public void setOrgInfoManager(OrgInfoManager manager) {
		this.orgInfoManager = manager;
	}
	@Autowired
	private TszbProcessInfoManager tszbProcessInfoManager;
	public void setTszbProcessInfoManager(TszbProcessInfoManager manager) {
		this.tszbProcessInfoManager = manager;
	}
	/**
	 * 超期督办
	 */
	public void unDealTszbProcessInfoOverTimeList(){
		log.info("工单超期督办提醒调度任务执行");
		UserInfoQuery uq=new UserInfoQuery();
		List<UserInfo> luis=userInfoManager.findAllWithSellPhoneIsNotNull(uq);
		for (UserInfo userInfo : luis) {
			TszbProcessInfoQuery query = new TszbProcessInfoQuery();
			query.setPageSize(1000);
			if(!userInfo.getSysUserType().equals(SysUserTypeEnum.BJ12320.getCode())){
				query.setOrgInfoId(orgInfoManager.getLevel1OrgInfo(userInfo.getOrgInfo()).getOrgInfoId());
			}else{
				continue;//12320用户暂不发送超时提醒
			}
			Page page = tszbProcessInfoManager.findPageWithUnDeal(query);
			List<TszbProcessInfo> ltzis=new ArrayList<TszbProcessInfo>();
			for (Iterator iter=page.getResult().iterator();iter.hasNext();) {
				TszbProcessInfo tpi=(TszbProcessInfo)iter.next();
				if(tpi.getIsDealTimeDueDateOvertimeString()){
					ltzis.add(tpi);
				}
			}
			if(ltzis.size()>0){
				SmsInfo si=new SmsInfo();
				si.setUserInfoId(userInfo.getUserInfoId());
				si.setIsGlobal(0L);
				si.setIsSent(0L);
				si.setSendDatetime(new Date());
				si.setSellPhone(userInfo.getSellPhone());
				si.setInfoContent("您有"+ltzis.size()+"件工单已超期，请速办理。联系电话：64468526。");
				smsInfoManager.save(si);
			}
		}
	}
}
